package com.gs.SynPatrolRep.activity.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.constant.AllConstants;
import com.gs.SynPatrolRep.constant.EventCode;
import com.gs.SynPatrolRep.control.ToolBarTitle;
import com.gs.SynPatrolRep.db.bean.Event;
import com.gs.SynPatrolRep.presenter.BasePresenter;
import com.gs.SynPatrolRep.util.ActivityUtil;
import com.gs.SynPatrolRep.util.LanguageUtils;
import com.gs.SynPatrolRep.util.WindowUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.ref.WeakReference;

public abstract class BaseEditActivity<V, T extends BasePresenter<V>> extends Activity {

    protected ToolBarTitle toolBarTitle;
    private WeakReference<Activity> weakReference = null;
    protected T mPresenter;
    protected String editId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(this.getLayoutId());
        WindowUtil.getInstance().setBarBgColor(this, R.color.blue);
        if (weakReference == null) {
            weakReference = new WeakReference<Activity>(this);
        }
        ActivityUtil.getInstance().addActivity(weakReference.get());
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
        Intent intent = getIntent();
        editId = intent.getStringExtra(AllConstants.ID);
        toolBarTitle = findViewById(R.id.toolBarTitle);
        if (toolBarTitle != null) {
            toolBarTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.item_back:
                            finish();
                            break;
                        case R.id.item_per:
                            save();
                            break;
                    }
                }
            });
        }
        initView();
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LanguageUtils.getInstance().change(LanguageUtils.getInstance().getCurrentLanguage()));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        if (event.getWhat() == EventCode.EVENT_LANGUAGE) {
            LanguageUtils.getInstance().change(LanguageUtils.getInstance().getCurrentLanguage());
            recreate();
        } else if (event.getWhat() == EventCode.EVENT_SITE) {
            recreate();
        }
    }

    public abstract void initView();

    public abstract void save();

    public abstract T createPresenter();

    protected void back(int status) {
        if (status == 2000) {
            setResult(1);
            finish();
        }
    }

    /**
     * 设置布局
     *
     * @return
     */
    public abstract int getLayoutId();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        EventBus.getDefault().unregister(this);
        ActivityUtil.getInstance().removeActivity(weakReference.get());
    }

    protected <I extends View> I fd(int id) {
        return findViewById(id);
    }
}